<template>
  <view class="deposit-page">

    <!-- 金额展示 -->
    <view class="amount-box">
      <text class="amount">￥{{money}}</text>
    </view>

    <!-- 温馨提示 -->
    <view class="tips">
      <text class="tips-title">温馨提示</text>
      <text class="tips-text">还车后，押金将自动原路退回至您的账户。</text>
    </view>

    <!-- 支付方式 -->
    <view class="pay-method">
      <image class="wechat-icon" src="https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/weier/weichat_pay.png"></image>
      <text class="wechat-text">微信支付</text>
      <text class="check-icon">✔</text>
    </view>

    <!-- 支付按钮 -->
    <view class="pay-btn" @tap="sub_order()">立即支付</view>
  </view>
</template>

<script>
import common_config from '../../static/js/common.js';
var config = common_config.config;
var that;	
var userInfo;
export default {
  name: "DepositPage",
  data(){
	   return {
		   czddRowid:'',
		   yajinRowid:'',
		   zujinRowid:'',
		   rowId:'',
		   money:'0.00',
		   pay_order:''
	   }
  },
  onLoad(options) {
  	that = this;
  	that.rowId = options.rowid;
  	that.czddRowid = options.czddRowid;
  	that.yajinRowid = options.yajinRowid;
  	that.zujinRowid = options.zujinRowid;
	that.money = options.money;
  	that.center = uni.getStorageSync('location_center');
  	//console.log(that.center);
  	that.center = JSON.parse(that.center);
  	that.plusReady();
  },
  methods: {
	  //初始化
	  plusReady(){
	    	userInfo = uni.getStorageSync('userInfo');
	    	if(userInfo){
	    		userInfo = JSON.parse(userInfo);
	    		that.if_login = true;
	    		that.load_datas();
	    	}else{
	  				uni.switchTab({
	  					url:'/pages/index/me'
	  				});
	    		that.if_login = false;
	    	}
	    	
	  },
    //提交支付
    sub_order(){
    	uni.showLoading({
    		title:'正在提交支付...'
    	})
      uni.request({
        url: config.weixin_url+'/api/Login/GetWxPrepayId',
        method: 'POST',
        header: { 'Content-Type': 'application/json' },
        data: {
          openId:userInfo.openid,
    	  totalMoney:that.money*100,
    	  billNo:that.pay_order,
    	  title:'押金',
    	  sign:'123455'
        },
        success: (res) => {
    		uni.hideLoading();
          res = res.data;
         if(res.status == 200){
    		 var datas = res.response;
    		 uni.requestPayment({
    		 	provider: 'wxpay',
    		 	orderInfo: that.pay_order,
    		 	timeStamp:datas.timeStamp,
    		 	nonceStr:datas.nonceStr,
    		 	package:datas.package,
    		 	signType:datas.signType,
    		 	paySign:datas.paySign,
    		 	success: function () {
    		 		uni.redirectTo({ url:'pay_suc?act=changzu&czddRowid='+that.czddRowid+'&yajinRowid='+that.yajinRowid+'&zujinRowid='+that.zujinRowid+'&rowid='+that.rowId });
    		 	},
    		 	fail: function (err) {
    		 		console.log(err);
    		 		common_config.calert('支付失败');
    		 	}
    		 });
    	 }else{
    		 common_config.calert(res.msg);
    	 }
        },
        fail: (err) => {
          console.error('请求失败:', err);
          uni.showToast({ title: '网络错误', icon: 'none' });
        }
      })  
    },
	load_datas(){
		//console.log(121212);
		uni.request({
		  url: config.request_url+'/v2/open/worksheet/getFilterRows',
		  method: 'POST',
		  header: {
			'Content-Type': 'application/json'
		  },
		  data: {
			"appKey": config.appKey,
			"sign": config.sign,
			"worksheetId": "zfjl",
			"pageSize": 50,
			"pageIndex": 1,
			"filters": [
				{
					"controlId": "rowid",
					"dataType": 2,
					"spliceType": 1,
					"filterType": 2,
					"value": that.yajinRowid// 支付记录rowid ，多条记录用英文逗号隔开
				}
			]
		  },
		  success: (res) => {
			//console.log('返回结果:', res.data);
			res = res.data;
			//console.log(res);
			if(res.success){
				//that.data_info = res.data;
				that.pay_order = res.data.rows[0].zfjlbh;
				//common_config.calert('测试'+that.pay_order);
				//common_config.calert(that.pay_order);
			}else{
				 uni.showToast({ title: res.data.msg || '数据获取失败', icon: 'none' });
			}
		  },
		  fail: (err) => {
			console.error('请求失败:', err);
			uni.showToast({ title: '网络错误', icon: 'none' });
		  }
		})  
	}
  }
};
</script>

<style>
.deposit-page{background:#fff;height:100vh;font-family:Arial;}
.nav-bar{display:flex;align-items:center;justify-content:space-between;padding:10px 15px;font-size:16px;position:relative;}
.back-icon{font-size:20px;}
.nav-title{position:absolute;left:50%;transform:translateX(-50%);font-weight:bold;}
.nav-right{display:flex;gap:10px;}
.amount-box{margin:20px auto;width:90%;background:linear-gradient(to right,#5dd5ff,#00aaff);border-radius:12px;padding:30px 0;text-align:center;}
.amount{color:#000;font-size:28px;font-weight:bold;}
.tips{margin:10px 20px;color:#666;font-size:14px;line-height:22px;}
.tips-title{font-weight:bold;margin-right:6px;}
.pay-method{display:flex;align-items:center;justify-content:space-between;padding:15px 20px;border-bottom:1px solid #f0f0f0;}
.wechat-icon{width:22px;height:22px;margin-right:10px;}
.wechat-text{flex:1;color:#333;font-size:16px;margin-left:8px;}
.check-icon{color:#06c25f;font-size:18px;}
.pay-btn{margin:40px 20px;background:#222;color:#fff;text-align:center;font-size:18px;padding:14px 0;border-radius:50px;}
</style>
